layui.use(['form','jquery','layer','table'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var table = layui.table;
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    getOrdersBySourcePay();
    $('#refresh').click(function () {
        location.reload()
    });
    //返回按钮
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    //高级检索
    $('#advancedSearch').click(function () {
        layer.open({
            type: 2,
            title: '高级检索',
            content: '/orderForm/salesReturnDispose/applyUnsubscribeList/order-advanced-search.html',
            area: ['710px', '405px'],
            offset: 'auto',
            shade: 0.4,
        })
    });
    //根据订单状态、订单来源、订单支付渠道查询订单
    form.on('submit(search)',function (obj) {
        getOrdersBySourcePay(obj.field);
        return false;
    });

    //收起按钮
    $('#packUp').click(function () {
        if($(this).text() == ' 收起'){
            $('.layui-card-body').slideUp(300,'linear');
            $('#packUp').text(' 展示').removeClass('layui-icon-down').addClass('layui-icon-up');
        }else if($(this).text() == ' 展示'){
            $('.layui-card-body').slideDown(300,'linear');
            $('#packUp').text(' 收起').removeClass('layui-icon-up').addClass('layui-icon-down');
        }
    })
    function getOrdersBySourcePay(SourcePay) {
        $.ajax({
            url: '/SyUserOrder/getUnsubscribeApplyOrdersBySourcePay',
            data: SourcePay,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                ordersRender(data.data.data);
            }
        });
    }
    //将查出来的数据渲染到表格中
    ordersRender = function ordersRender(data) {
        table.render({
            elem: '#ordersTable',
            title: '审核列表',
            data: data,
            limit: 10,
            width: 1630,
            size: 'lg',
            toolbar: '#button_ids',
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                //limits:[3,6,9],
                first:"首页",
                last:"尾页",
            },
            cols: [[
                {type:'checkbox',fixed:'left'},
                {title:'序号',align:'center',width:100,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"orderLogisticsNumber",title:"订单号",align:'center',width:170},
                {field:"beforeAccount",title:"用户昵称",align:'center',width:170},
                {field:"beforePhone",title:"用户手机",align:'center',width:170},
                {field:"orderStatus",title:"订单状态",align:'center',width:170,templet:function(data){
                    if(data.orderStatus == 7){
                        return '<p>申请退订中</p>';
                    }
                }},
                {field:"",title:"商品/课程名字",align:'center',width:180,templet:function (data) {
                    if(data.orderType == 2){
                        return data.courseName;
                    }else if(data.orderType == 3){
                        return data.productName;
                    }
                }},
                {field:"",title:"现价",align:'center',width:180,templet:function (data) {
                    if(data.orderType == 2){
                        return data.courseNprice;
                    }else if(data.orderType == 3){
                        return data.currentpriceProduct;
                    }
                }},
                {field:"",title:"类型",align:'center',width:100,templet:function (data) {
                    if(data.orderType == 2){
                        return '课程';
                    }else if(data.orderType == 3){
                        return '商品';
                    }
                }},
                {field:"orderPaymentChannel",title:"支付渠道",align:'center',templet:function (data) {
                    if(data.orderPaymentChannel == 2){
                        return '微信支付';
                    }else if(data.orderPaymentChannel == 3){
                        return '余额支付';
                    }
                }},
                {field:'',title:"操作",align:'center',width:180,toolbar: '#operation'}
            ]]
        });
    }
    table.on('tool(ordersTable)',function (data) {
        if(data.event == 'deleteTable'){
            var orderName;
            if(data.data.orderType == 2){
                orderName = data.data.courseName;
            } else if(data.data.orderType == 3){
                orderName = data.data.productName;
            }
            delCurrentOrder(data.data.orderId,orderName);
        }else if(data.event == 'selectTable'){
            if(data.data.orderType == 3){
                layer.open({
                    type: 2,
                    title: '订单详情',
                    content: '/orderForm/productOrder/productOrderList/product-detailed-information.html',
                    area:["95%","99%"],
                    scrollbar: false,
                    offset:'auto',
                    shade: 0.4,
                    btn:['确定','取消'],
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    success:function(layero,index) {
                        getProductByOrderId(data.data.orderId,layero);
                        getProductBySalesVolume(data.data.shopId,layero)
                    }

                })
            }else if(data.data.orderType == 2){
                layer.open({
                    type: 2,
                    title: '订单详情',
                    content: '/orderForm/courseOrder/courseOrderList/course-detailed-information.html',
                    area:["95%","99%"],
                    scrollbar: false,
                    offset:'auto',
                    shade: 0.4,
                    btn:['确定','取消'],
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    success:function(layero,index) {
                        getCourseByOrderId(data.data.orderId,layero);
                    }

                })
            }
        }
    })
    //删除当前行订单
    function delCurrentOrder(orderId,orderName) {
        var layerIndex = layer.confirm('确定要删除"' + orderName + '"么?', {
            btn: ['确定','取消'],
            icon: 3,
            title: '提示'
        }, function(){
            $.ajax({
                url: '/SyUserOrder/deleteCurrentOrder',
                data: {orderId: orderId},
                type: 'post',
                dataType: 'json',
                success:function (data) {
                    layer.msg(data.data);
                    getOrdersBySourcePay();
                }
            });
            layer.close(layerIndex);
        }, function(){
            layer.close(layerIndex);
        });
    }
    //获取商品销量
    function getProductBySalesVolume(shopId,layero) {
        $.ajax({
            url: '/product/getProductBySalesVolume',
            dataType: 'json',
            type: 'post',
            data:{shopId: shopId},
            success: function (data) {
                var product = data.data;
                var childHtml = layero.find('iframe').contents();
                childHtml.find('#productSales').text(product.productSales);
            }
        })
    }
    //查看当前订单有关信息
    function getProductByOrderId(orderId,layero) {
        $.ajax({
            url: '/SyUserOrder/getProductByOrderId',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                var product = data.data;
                var childHtml = layero.find('iframe').contents();
                ifUseCoupon(orderId,childHtml,product);
                childHtml.find('#orderLogisticsNumber').text(product.orderLogisticsNumber);
                switch (product.orderStatus) {
                    case 2 :
                        product.orderStatus = '未付款';
                        break;
                    case 3 :
                        product.orderStatus = '已付款';
                        break;
                    case 10 :
                        product.orderStatus = '取消交易';
                        break;
                }
                switch (product.orderType) {
                    case 2 :
                        product.orderType = '视频';
                        break;
                    case 3 :
                        product.orderType = '商品';
                        break;
                }
                switch (product.orderSource) {
                    case 2 :
                        product.orderSource= '小程序';
                        break;
                    case 3 :
                        product.orderSource = 'app';
                        break;
                }
                childHtml.find('#orderStatus').text(product.orderStatus);
                childHtml.find('#orderType').text(product.orderType);
                childHtml.find('#orderSource').text(product.orderSource);
                childHtml.find('#orderPaymentChannel').text(product.orderPaymentChannel);
                childHtml.find('#currentpriceProduct').text(product.currentpriceProduct + '元');
                childHtml.find('#account').text(product.account);
                childHtml.find('#nickname').text(product.nickname);
                childHtml.find('#phone').text(product.phone);
                childHtml.find('#email').text(product.email);
                childHtml.find('#productName').text(product.productName);
                childHtml.find('#className').text(product.className);
                childHtml.find('#productDescription').text(product.productDescription);
                childHtml.find('#mainImage').attr('src',product.mainImage);
            }
        });
    }
    //查看当前订单有关信息
    function getCourseByOrderId(orderId,layero) {
        $.ajax({
            url: '/SyUserOrder/getSyUserOrderById',
            data: {orderId: orderId},
            type: 'post',
            dataType: 'json',
            success:function (data) {
                var course = data.data;
                var childHtml = layero.find('iframe').contents();
                ifUseCoupon(orderId,childHtml,course);
                childHtml.find('#orderLogisticsNumber').text(course.orderLogisticsNumber);
                switch (course.orderStatus) {
                    case 2 :
                        course.orderStatus = '未付款';
                        break;
                    case 3 :
                        course.orderStatus = '已付款';
                        break;
                    case 10 :
                        course.orderStatus = '取消交易';
                        break;
                }
                switch (course.orderType) {
                    case 2 :
                        course.orderType = '视频';
                        break;
                    case 3 :
                        course.orderType = '商品';
                        break;
                }
                switch (course.orderSource) {
                    case 2 :
                        course.orderSource= '小程序';
                        break;
                    case 3 :
                        course.orderSource = 'app';
                        break;
                }
                childHtml.find('#orderStatus').text(course.orderStatus);
                childHtml.find('#orderType').text(course.orderType);
                childHtml.find('#orderSource').text(course.orderSource);
                childHtml.find('#orderPaymentChannel').text(course.orderPaymentChannel);
                childHtml.find('#courseNprice').text(course.courseNprice + '元');
                childHtml.find('#account').text(course.account);
                childHtml.find('#nickname').text(course.nickname);
                childHtml.find('#phone').text(course.phone);
                childHtml.find('#email').text(course.email);
                childHtml.find('#courseName').text(course.courseName);
                childHtml.find('#className').text(course.className);
                childHtml.find('#courseTime').text(course.courseTime + '分钟');
                childHtml.find('#courseInfo').text(course.courseInfo);
                childHtml.find('#url').attr('src',course.url);
                childHtml.find('#courseUrl').attr('src',course.courseUrl);
            }
        });
    }
    function ifUseCoupon(orderId,childHtml,course) {
        $.ajax({
            url: '/JsUserCoupon/getUserCouponPrice',
            data:{orderId:orderId},
            dataType:'json',
            success:function (data) {

                if(data.data != null && data.data != '' && data.data != undefined){
                    childHtml.find('#couponPrice').text(data.data + '元');
                    childHtml.find('#coursePrice').text((parseInt(course.courseNprice) - parseInt(data.data)) + '元');
                }else{
                    childHtml.find('#couponPrice').text('此订单未使用优惠券');
                    childHtml.find('#coursePrice').text(course.courseNprice + '元');
                }
            }
        })
    }
})